package com.cherry.framework.config;

import com.cherry.common.config.CherryConfig;
import com.cherry.common.constant.Constants;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * 通用配置
 *
 * @author 孙晨阳
 */
@Configuration
public class ResourcesConfig implements WebMvcConfigurer {

    @Value("${shiro.user.indexUrl}")
    private String indexUrl;

    /**
     * 默认首页的设置，当输入域名是可以自动跳转到默认指定的网页
     */
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/").setViewName("forward:" + indexUrl);
    }

    /**
     * springboot中配置addResourceHandler和addResourceLocations来访问本地的图片
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        // 将上传到本地的文件映射到springboot服务器中，可以由域名开始进行访问
        registry.addResourceHandler(Constants.RESOURCE_PREFIX + "/**").addResourceLocations("file:" + CherryConfig.getProfile() + "/");
    }
}
